package com.bytedance.danmaku.render.engine.demo.danmu

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.View
import com.bytedance.danmaku.render.engine.DanmakuView
import com.bytedance.danmaku.render.engine.control.DanmakuConfig
import com.bytedance.danmaku.render.engine.control.DanmakuController
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.utils.dp
import com.bytedance.danmaku.render.engine.render.draw.IDrawItemFactory
import com.bytedance.danmaku.render.engine.render.draw.bitmap.BitmapData
import com.bytedance.danmaku.render.engine.touch.IItemClickListener
import com.bytedance.danmaku.render.engine.utils.LAYER_TYPE_BOTTOM_CENTER
import com.bytedance.danmaku.render.engine.utils.LAYER_TYPE_SCROLL
import com.bytedance.danmaku.render.engine.utils.LAYER_TYPE_TOP_CENTER

/**
 * Created by Yanxin on 2025/9/1
 * 弹幕接口实现类
 */
class DanmakuViewImpl : IDanmakuView {
    private var mDanmakuView: DanmakuView? = null
    private var mDanmakuController: DanmakuController? = null
    private var mDanmakuDataList = mutableListOf<DanmakuData>()
    override fun init(danmakuView: DanmakuView) {
        mDanmakuView = danmakuView
        mDanmakuController = mDanmakuView?.controller
    }

    override fun isShowDanmakuView(): Boolean {
        return mDanmakuView?.visibility == View.VISIBLE
    }

    override fun showDanmakuView() {
        mDanmakuView?.visibility = View.VISIBLE
    }

    override fun hideDanmakuView() {
        mDanmakuView?.visibility = View.INVISIBLE
    }

    override fun setData(danmakuList: List<DanmakuData>) {
        mDanmakuDataList.clear()
        mDanmakuDataList.addAll(danmakuList)
        mDanmakuController?.setData(mDanmakuDataList)
    }

    override fun getDanmakuConfig(): DanmakuConfig? {
        return mDanmakuController?.config
    }

    override fun getDanmakuStroll(): DanmakuConfig.ScrollLayerConfig? {
        return mDanmakuController?.config?.scroll
    }

    override fun start() {
        mDanmakuController?.start()
    }

    override fun pause() {
        mDanmakuController?.pause()
    }

    override fun resume(currentTime: Long) {
        mDanmakuController?.start(currentTime)
    }

    override fun stop() {
        mDanmakuController?.stop()
    }

    override fun addDanmaku(item: DanmakuData) {
        mDanmakuController?.addFakeData(item.apply {
            layerType = LAYER_TYPE_SCROLL
        })
    }

    override fun updateDanmakuView() {
        mDanmakuController?.invalidateView()
    }

    override fun seekTo(playTime: Long) {
        mDanmakuController?.pause()
        mDanmakuController?.start(playTime)
    }

    override fun setOnDanmakuClickListener(listener: IItemClickListener) {
        mDanmakuController?.itemClickListener = listener
    }

    override fun setAlpha(alpha: Float) {
        mDanmakuController?.config?.common?.alpha = (255 * alpha).toInt()
    }

    override fun setLineCount(lineCount: Int) {
        mDanmakuController?.config?.scroll?.lineCount = lineCount
    }

    override fun sendTopDanmakuData(item: DanmakuData) {
        mDanmakuController?.addFakeData(item.apply {
            layerType = LAYER_TYPE_TOP_CENTER
        })
    }

    override fun sendBottomDanmakuData(item: DanmakuData) {
        mDanmakuController?.addFakeData(item.apply {
            layerType = LAYER_TYPE_BOTTOM_CENTER
        })
    }

    override fun sendBitmapDanmakuData(
        bitmapWidth: Float,
        bitmapHeight: Float,
        resource: Bitmap,
    ) {
        mDanmakuController?.addFakeData(BitmapData().apply {
            width = bitmapWidth
            height = bitmapHeight
            bitmap = resource
            layerType = LAYER_TYPE_SCROLL
        })
    }

    override fun registerDrawItemFactory(factory: IDrawItemFactory) {
        mDanmakuController?.registerDrawItemFactory(factory)
    }

    override fun setTextSize(textSize: Float) {
        mDanmakuController?.config?.text?.size = textSize
    }

    override fun setSpeed(speed: Float) {
        mDanmakuController?.config?.common?.playSpeed = (100 * speed).toInt()
        mDanmakuController?.config?.scroll?.moveTime = (8000L / speed).toLong()
    }

    override fun clear() {
        mDanmakuDataList.clear()
        mDanmakuController?.clear()
    }

    override fun release() {
        mDanmakuController?.stop()
        mDanmakuDataList.clear()
        mDanmakuController = null
        mDanmakuView = null
    }
}